[iOS 10] User Notifications framework を使用してアプリの通知の設定を取得する

[iOS 10] User Notifications framework を使用してアプリの通知の設定を取得する

Clock Icon2016.09.14

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

こんにちは。モバイルアプリサービス部の平屋です。前回の記事に引き続き、iOS 10 で追加された「User Notifications framework」のクラスを使用した実装を紹介します。

iOS 標準の「設定アプリ」->「通知」->「(アプリ名)」を開くと、そのアプリの通知設定画面を表示することができます。本記事では、ここに表示されている通知設定の値を取得する方法を解説していきます。

user-notifications-framework-14-1

通知設定を取得する

通知設定を取得するには、UNUserNotificationCenter クラスの getNotificationSettings(completionHandler:) メソッドを使用します。

取得完了時に呼ばれる completionHandler の引数が UNNotificationSettings オブジェクトとなっているので、このオブジェクトから設定情報を取得できます。

以下の例では、通知の認証状態をログに出力しています。

UNUserNotificationCenter.current().getNotificationSettings { (settings) in
    switch settings.authorizationStatus {
    case .authorized:
        print("authorized")
    case .denied:
        print("denied")
    default:
        print("notDetermined")
    }
}

上記の実装例では「通知の認証状態」だけ取り出しましたが、UNNotificationSettings オブジェクトには他の設定値も格納されています。

  • UNNotificationSettings
    • authorizationStatus
    • notificationCenterSetting
    • lockScreenSetting
    • carPlaySetting
    • alertSetting
    • alertStyle
    • badgeSetting
    • soundSetting

通知設定画面と UNNotificationSettings の各プロパティとの対応関係

user-notifications-framework-14-2

通知設定画面と UNNotificationSettings の各プロパティとの対応関係は以下の通りです。

セクション 0

設定.app上の項目 対応する UNNotificationSettings のプロパティ
通知を許可 authorizationStatus

セクション 1

設定.app上の項目 対応する UNNotificationSettings のプロパティ
通知センターに表示 notificationCenterSetting
サウンド soundSetting
Appアイコンにバッジを表示 badgeSetting
ロック画面に表示 lockScreenSetting

セクション 2

設定.app上の項目 対応する UNNotificationSettings のプロパティ
ロックされていないときの通知のスタイル alertStyle, alertSetting

スタイル、alertStyle、alertSetting の対応関係

選択中のスタイル alertStyle の値 alertSetting の値
なし none disabled
バナー banner enabled
通知 alert enabled

さいごに

本記事では、通知設定の値を取得する方法を紹介しました。

通知の設定値取得を実装する際の参考になれば幸いです!

参考資料

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.